package argo.hot100.linklist;

import java.util.HashSet;
import java.util.Set;

public class DetectCycle {

    public ListNode detectCycle(ListNode head) {

        ListNode p = head;
        Set<ListNode> set = new HashSet<>();

        while (head != null && head.next != null) {
            if (set.contains(head)) {
                return head;
            }
            set.add(head);
            head = head.next;

        }


        return null;

    }
}
